package models.re.simulatedData;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;

import models.aaa.Node;
import models.re.IItem;
import models.re.IRelation;

@Entity
public class Relation extends Node implements IRelation {
	
	
	public enum Type {
		EMPTY, CREATE, VISITE, LIKE, IGNORE, FAVORITE, ACTION, BELONG;
    }

	private Type type = Type.EMPTY;
	
	private Integer count = 0;
	
	@ManyToOne(fetch=FetchType.LAZY)
	private Item source = null;
	
	@ManyToOne(fetch=FetchType.LAZY)
	private Item target = null;
	
	public static Type str2Relation(String type) {
		if (type.toLowerCase().indexOf("create") >= 0) {
            return Type.CREATE;
        } else if (type.toLowerCase().indexOf("visite") >= 0) {
            return Type.VISITE;
        } else if (type.toLowerCase().indexOf("like") >= 0) {
            return Type.LIKE;
        } else if (type.toLowerCase().indexOf("ignore") >= 0) {
            return Type.IGNORE;
        } else if (type.toLowerCase().indexOf("favorite") >= 0) {
            return Type.FAVORITE;
        } else if (type.toLowerCase().indexOf("action") >= 0) {
            return Type.ACTION;
        } else if (type.toLowerCase().indexOf("belong") >= 0) {
            return Type.BELONG;
        } else {
            return Type.EMPTY;
        }
	}
	
	Relation() {
		
	}
	
	Relation(IItem source, IItem target, String type) {
        this.type=str2Relation(type);
        this.source=(Item)source;
        this.target=(Item)target;       
        this.increment();
	}

	/* (non-Javadoc)
	 * @see models.re.simulatedData.IRelation#getType()
	 */
	@Override
	public Type getType() {
		return this.type;
	}
	
	/* (non-Javadoc)
	 * @see models.re.simulatedData.IRelation#increment()
	 */
	@Override
	public void increment() {
		if(this.type==Type.VISITE) this.count+=1;
	}

	/* (non-Javadoc)
	 * @see models.re.simulatedData.IRelation#getSource()
	 */
	@Override
	public IItem getSource() {
		return source;
	}

	/* (non-Javadoc)
	 * @see models.re.simulatedData.IRelation#getTarget()
	 */
	@Override
	public IItem getTarget() {
		return target;
	}

	/* (non-Javadoc)
	 * @see models.re.simulatedData.IRelation#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.count;
	}


}